<?php require_once LAYOUTPATH.'layout_header.php'; ?>

<h1><?php echo $page_title; ?></h1>

<div id="bricabox_tabs" class="admin">
	<ul>
		<li class="back"><a href="/">&lsaquo; Back to your BricaBox</a></li>
		<?php render_tabs($admin_tabs); ?>
	</ul>
</div>

<?php require_once FLASHPARTIAL; ?>

<form action="/bricabox/<?php echo $bricabox->id; ?>/save" method="post" onsubmit="return checkForm();">
	<input type="hidden" name="section" value="members" />
	<fieldset>
		<div>
			<label for="passcode"><span class="req">*</span>Choose a passcode for this BricaBox:</label><br />
			<input type="text" id="passcode" name="bricabox[invite_code]" value="<?php echo $bricabox->invite_code; ?>" />
		</div>
		
		<div class="controls">
			<input type="submit" value="Save" />
		</div>
	</fieldset>
</form>

<script type="text/javascript" charset="utf-8">
	function checkForm()
	{
		var el = $('#passcode');
		if (!el.val())
		{
			alert("You must set a passcode.");
			el.focus();
			return false;
		}
	}
</script>

<table>
	<thead>
		<tr>
			<td>Username</td>
			<td>Role</td>
			<td>Actions</td>
		</tr>
	</thead>
	<tbody>
		<?php
		foreach ($pending->result() as $member):
		
		?>
		<tr class="<?php echo alternator('even', 'odd'); ?>">
			<td><span class="toe_tag">Pending</span> <?php echo username($member); ?> (<?php echo $member->login; ?>)</td>
			<td>&mdash;</td>
			<td>
				<form id="approve_<?php echo $member->id; ?>" method="post" action="/admin/add_member" style="display: inline;">
					<input type="hidden" name="bricabox_id" value="<?php echo $bricabox->id; ?>" />
					<input type="hidden" name="username_or_email" value="<?php echo $member->login; ?>" />
				</form>
				<a href="#" onclick="document.getElementById('approve_<?php echo $member->id; ?>').submit();return false;">Approve</a>
				<form id="deny_<?php echo $member->id; ?>" method="post" action="/admin/remove_application" style="display: inline;">
					<input type="hidden" name="bricabox_id" value="<?php echo $bricabox->id; ?>" />
					<input type="hidden" name="login" value="<?php echo $member->login; ?>" />
				</form>
				or <a href="#" onclick="if (confirm('Are you sure?')) {document.getElementById('deny_<?php echo $member->id; ?>').submit();return false;}">Deny</a>
			</td>
		</tr>
		<?php
		endforeach;
		?>
		<?php
		foreach ($members->result() as $member):
			$membership = $this->Membership_model->is_member_of($BBX['bricabox']->id, $member->id);
			$role		= $this->Membership_model->get_role_for($membership->role_id);
			if ($role->name != 'Owner'):
		?>
		<tr class="<?php echo alternator('even', 'odd'); ?>" id="role_for_<?php echo $membership->id; ?>">
			<td><?php echo username($member); ?> (<?php echo $member->login; ?>)</td>
			<td><?php echo $role->name; ?></td>
			<td>
				<a href="#" class="edit" onclick="return Admin.startEditRole(<?php echo $membership->id; ?>);">Edit role</a>
				<a href="/admin/remove_member/<?php echo $membership->bricabox_id; ?>/<?php echo $member->id; ?>" class="delete" title="Remove member" onclick="return confirm('Are you sure?');">
					<img src="/assets/images/icons/cross.png" alt="Remove member" />
				</a>
			</td>
		</tr>
		<tr class="edit" id="edit_role_for_<?php echo $membership->id; ?>" style="display: none;">
			<td><?php echo $member->login; ?></td>
			<td>currently a <?php echo $role->name; ?></td>
			<td>
				<form action="/admin/change_member_role" method="post">
					<input type="hidden" name="bricabox_id" value="<?php echo $membership->bricabox_id; ?>" />
					<input type="hidden" name="user_id" value="<?php echo $member->id; ?>" />
					<label for="change_role_<?php echo $membership->id; ?>">Make this member a</label>
					<select id="change_role_<?php echo $membership->id; ?>" name="change_role">
						<?php if ($role->id != 3): ?><option value="3">Member</option><?php endif; ?>
						<?php if ($role->id != 4): ?><option value="4">Moderator</option><?php endif; ?>
					</select>
					instead.
					<input type="submit" value="Change" />
					or <a href="#" onclick="return Admin.endEditRole(<?php echo $membership->id; ?>);">cancel</a>
				</form>
			</td>
		</tr>
		<?php
			endif;
		endforeach;
		?>
		<tr class="bottom" id="role_for_new">
			<td><a href="#" class="new" onclick="return Admin.startEditRole('new');">Add a new member to this BricaBox</a></td>
			<td></td>
			<td></td>
		</tr>
		<tr class="edit" id="edit_role_for_new" style="display: none;">
			<td>
				<form action="/admin/add_member" method="post" onsubmit="return Admin.checkAddMember();">
					<input type="hidden" name="bricabox_id" value="<?php echo $membership->bricabox_id; ?>" />
					<fieldset>
						<div>
							<label for="username_or_email">Tell us the username or email address of the person you'd like to make a member of <?php echo $BBX['bricabox']->name; ?>:</label><br />
							<input type="text" id="username_or_email" name="username_or_email" value="" />
						</div>
						
						<div class="controls">
							<input type="submit" value="Add member" />
							or <a href="#" onclick="return Admin.endEditRole('new');">cancel</a>
						</div>
					</fieldset>
				</form>
			</td>
			<td></td>
			<td></td>
		</tr>
	</tbody>
</table>

<div class="big_info_box">
	<h4>How do I add and manage members?</h4>
	<p>This screen lets you add new members and manage their rights in your BricaBox. Start by pressing the big green button to add or invite a member. Also, if your BricaBox is private, you can pick a new passcode (above) and save it. Lastly, you can change and edit the permissions of each member by clicking "Edit Role."</p>
</div>

<?php require_once LAYOUTPATH.'layout_footer.php'; ?>